package com.zepher.springboot.controller;

import com.github.pagehelper.PageInfo;
import com.zepher.springboot.common.Result;
import com.zepher.springboot.entity.User;

import com.zepher.springboot.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询所有用户
     * @param user
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(User user) {
        List<User> list = userService.selectAll(user);
        return Result.success(list);
    }

    /**
     * 根据用户ID查询单个用户
     * @param id 用户ID
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable  Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(User user,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<User> pageInfo = userService.selectPage(pageNum, pageSize, user);
        return Result.success(pageInfo);
    }

    /**
     * 新增用户
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody User user) {
        userService.insert(user);
        return Result.success();
    }

    /**
     * 修改用户
     */
    @PutMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.delete(id);
        return Result.success();
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatch(ids);
        return Result.success();
    }
}
